package com.test.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @packageName: com.test.util
 * @name: EmailUtils
 * @description:
 * @author: 董定卓
 * @dateTime: 2018/9/18 10:56
 */
@Component
public class EmailUtils {
    @Autowired
    private JavaMailSender mailSender;

    public static EmailUtils emailUtils;

    @PostConstruct
    public void init() {
        emailUtils = this;
    }

    /**
     * 发送邮件
     *
     * @param from
     * @param to
     * @param topic
     * @param content
     */
    public static void sendEmail(String from, String to, String topic, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject("主题：" + topic);
        message.setText(content);
        emailUtils.mailSender.send(message);
    }
}
